package com.credithc.signature.manager.service;

import com.credithc.baseserv.pageHelper.PageInfo;
import com.credithc.signature.common.mapper.CallChannelHourMapper;
import com.credithc.signature.common.mapper.CallHourMapper;
import com.credithc.signature.common.mapper.CallStatisticsMapper;
import com.credithc.signature.common.po.CallChannelHour;
import com.credithc.signature.common.po.CallHour;
import com.credithc.signature.common.po.CallStatistics;
import com.credithc.signature.common.ro.manager.ChannelRo;
import com.credithc.signature.common.service.ChannelService;
import com.credithc.signature.manager.ro.ProductRo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author hanqiu zhang
 */
@Service
@Slf4j
public class StatisticsSerivce {

    @Autowired
    private CallHourMapper callHourMapper;
    @Autowired
    private CallChannelHourMapper callChannelHourMapper;
    @Autowired
    private ChannelService channelService;
    @Autowired
    private CallStatisticsMapper callStatisticsMapper;
    @Autowired
    CompanyService companyService;

    /**
     * 记录调用
     * @param channelNo
     */
    @Async
    @Deprecated
    public void statisticsCall(String channelNo){
        String timeId = getTimeId();
        bulidDayDate();
        try {
            callHourMapper.addNum(timeId);
        } catch (Exception e) {

        }
        bulidDayDate("");
        try {
            callChannelHourMapper.addNum(timeId,channelNo);
        } catch (Exception e) {
        }
    }

    /**
     * 获取当前的时间id
     * @return
     */
    private String getTimeId() {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHH");
        return format.format(new Date());
    }

    public void bulidDayDate(){
        String timeId = getTimeId();
        Example example = new Example(CallHour.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("timeId",timeId);
        List<CallHour> callHours = callHourMapper.selectByExample(example);
        if (callHours != null &&callHours.size()<1){
            CallHour callHour = new CallHour();
            callHour.setNum(0);
            callHour.setUpdateTime(new Date());
            String substring = timeId.substring(0, 8);
            String time = substring + "00";
            Integer t = Integer.valueOf(time);
            for (int i = 0;i<=23;i++) {
                callHour.setTimeId(t+i+"");
                try {
                    callHourMapper.insert(callHour);
                } catch (Exception e) {

                }
            }
        }
    }

    public void bulidDayDate(String channelNo){
        String timeId = getTimeId();
        Example example = new Example(CallChannelHour.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("timeId",timeId);
        if (StringUtils.isNotBlank(channelNo)) {
            criteria.andEqualTo("channelNo", channelNo);
        }
        List<CallChannelHour> callChannelHours = callChannelHourMapper.selectByExample(example);
        if (callChannelHours.isEmpty()){
            ChannelRo channelRo = new ChannelRo();
            channelRo.setPageNo(1);
            channelRo.setPageSize(Integer.MAX_VALUE);
            PageInfo<ProductRo> page = companyService.getProductList(null);
            List<ProductRo> list = page.getList();
            if (list == null || list.size()<1){
                return;
            }
            CallChannelHour callChannelHour = new CallChannelHour();
            callChannelHour.setNum(0);
            callChannelHour.setUpdateTime(new Date());
            String substring = timeId.substring(0, 8);
            String time = substring + "00";
            Integer t = Integer.valueOf(time);
            for (int i = 0;i<=23;i++) {
                callChannelHour.setTimeId(t+i+"");
                for (ProductRo c:list) {
                    callChannelHour.setChannelNo(c.getCode());
                    callChannelHour.setChannelName(c.getProductName());
                    try {
                        callChannelHourMapper.insert(callChannelHour);
                    } catch (Exception e) {

                    }
                }
            }
        }
    }

    public void bulidChannelDayDate(String timeId) {
        ChannelRo channelRo = new ChannelRo();
        channelRo.setPageNo(0);
        channelRo.setPageSize(Integer.MAX_VALUE);
        PageInfo<ProductRo> page = companyService.getProductList(null);
        List<ProductRo> list = page.getList();
        for (ProductRo c:list) {
            CallStatistics callStatistics = new CallStatistics();
            callStatistics.setTimeId(timeId);
            callStatistics.setStatus(0);
            callStatistics.setChannelNo(c.getCode());
            callStatistics.setNum(0);
            try {
                callStatisticsMapper.insert(callStatistics);
            } catch (Exception e) {
                log.error("统计类StatisticsSerivce[bulidChannelDayDate-1]方法发生异常", e);
            }
            callStatistics.setStatus(1);
            try {
                callStatisticsMapper.insert(callStatistics);
            } catch (Exception e) {
                log.error("统计类StatisticsSerivce[bulidChannelDayDate-2]方法发生异常", e);
            }
        }
    }
}
